package com.zhou.project.modules.circle.circleGroupPosts.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.project.modules.circle.circleGroup.service.CircleGroupService;
import com.zhou.project.modules.circle.circleGroupPosts.entity.CircleGroupPosts;
import com.zhou.project.modules.circle.circleGroupPosts.service.CircleGroupPostsService;
import com.zhou.project.modules.circle.circlePostsComment.controller.CircleCommentController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.crypto.Mac;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@Api(tags = "[圈子话题]")
@RequestMapping("circle/posts")
public class CircleGroupPostsController {

    @Autowired
    private CircleGroupPostsService circleGroupPostsService;
    @Autowired
    private CircleCommentController circleCommentController;

    @PostMapping
    @ApiOperation("话题创建")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "话题名称",required = true,dataTypeClass = String.class),
            @ApiImplicitParam(name = "introduce",value = "帖子内容",required = true,dataTypeClass = String.class),
            @ApiImplicitParam(name = "circleTypeId",value = "帖子类型id",required = true,dataTypeClass = Integer.class)
    })
    @PreAuthorize("hasAuthority('posts:save')")
    public void save(@RequestBody @Valid CircleGroupPosts entity)
    {
        circleGroupPostsService.save(entity);
    }

    @PutMapping
    @ApiOperation("话题修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "话题名称",required = true,dataTypeClass = String.class),
            @ApiImplicitParam(name = "introduce",value = "帖子内容",required = true,dataTypeClass = String.class),
            @ApiImplicitParam(name = "circleTypeId",value = "帖子类型id",required = true,dataTypeClass = Integer.class)
    })
    @PreAuthorize("hasAuthority('posts:update')")
    public void update(@RequestBody @Valid CircleGroupPosts entity)
    {
        circleGroupPostsService.updateById(entity);
    }

    @DeleteMapping("{id}")
    @ApiOperation("话题删除")
    @ApiImplicitParam(name = "id",value = "话题id",required = true,dataTypeClass = Integer.class)
    @PreAuthorize("hasAuthority('posts:delete')")
    public void delete(@PathVariable @NotNull(message = "id不能为空") int id)
    {
      circleGroupPostsService.removeById(id);
        circleCommentController.deleteAll(id);

    }

    @DeleteMapping("dbatch")
    @ApiOperation("话题批量删除")
    @ApiImplicitParam(name = "ids",value = "话题id集合",required = true,dataTypeClass = List.class)
    @PreAuthorize("hasAuthority('posts:dbatch')")
    public void delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空") List<Integer> ids)
    {
        circleGroupPostsService.removeByIds(ids);
    }

    @GetMapping("{id}")
    @ApiOperation("话题单个查询")
    @ApiImplicitParam(name = "id",value = "话题id",required = true,dataTypeClass = Integer.class)
    @PreAuthorize("hasAuthority('posts:query')")
    public CircleGroupPosts query(@PathVariable @NotNull(message = "id不能为空") int id)
    {
        return circleGroupPostsService.getById(id);
    }

    @GetMapping("page")
    @ApiOperation("话题分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "话题名称",required = false,dataTypeClass = String.class),
            @ApiImplicitParam(name = "introduce",value = "帖子内容",required = false,dataTypeClass = String.class),
            @ApiImplicitParam(name = "circleTypeId",value = "帖子类型id",required = false,dataTypeClass = Integer.class)
    })
    @PreAuthorize("hasAuthority('posts:page')")
    public Page<CircleGroupPosts> query(@RequestBody CircleGroupPosts entity)
    {
        return circleGroupPostsService.page(entity);
    }


}
